<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Data filter chain
 *
 * Represents a chain of data filters.
 */
class entityresolution_DataFilterChain
{
	/**
	 * Data filters.
	 * @var array[int]entityresolution_DataFilter
	 */
	protected $dataFilters;
	
	/**
	 * Create a new data filter chain instance.
	 * @param array[int]entityresolution_DataFilter $dataFilters data filters
	 */
	public function __construct($dataFilters)
	{
		$this->dataFilters = $dataFilters;
	}
	
	/**
	 * Filter the source data in an entity using the chain of data filters.
	 * @param entityresolution_Entity $entity entity
	 */
	public function filterSourceData($entity)
	{
		// fetch the source data
		$sourceData = $entity->getSourceData();
		$sourceDataResource = $sourceData->getResourceBySubject($entity->getSubjectUri());

		// fill the entity property values with the source data
		$properties = array();
		$predicates = $sourceDataResource->getPredicates();
		foreach($predicates as $predicate)
		{
			$values = array();
			$objects = $sourceDataResource->getObjects($predicate);
			foreach($objects as $object)
			{
				$value = new entityresolution_Value();
				$value->setValue($object);
				$value->setValueForms(array($object));
				$values[] = $value;
			}
			
			$property = new entityresolution_Property();
			$property->setPredicate($predicate);
			$property->setPredicateForms(array($predicate));
			$property->setValues($values);
			$properties[] = $property;
		}
		
		// set the properties
		$entity->setProperties($properties);
		
		// call all data filters in the chain
		foreach($this->dataFilters as $dataFilter)
		{
			/* @var $dataFilter entityresolution_DataFilter */
			$dataFilter->filterData($entity);
		}
	}
}